package com.tpu.osu.rating.service;

import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.tpu.osu.rating.Vote;

public class VoteServiceAssync implements VoteService, MessageListener
{

    private final static Log log = LogFactory.getLog("rating.vote-serice-assync");

    private JmsTemplate jmsTemplate;

    private VoteService service;

    public void setJmsTemplate(JmsTemplate aJmsTemplate)
    {
        jmsTemplate = aJmsTemplate;
    }

    public void setService(VoteService aService)
    {
        service = aService;
    }

    public void create(final Vote aVote) throws Exception
    {
        jmsTemplate.send(new MessageCreator()
        {
            public Message createMessage(Session aSession) throws JMSException
            {
                return aSession.createObjectMessage(aVote);
            }
        });
    }

    public void onMessage(Message aMessage)
    {
        try
        {

            ObjectMessage msg = (ObjectMessage) aMessage;
            Vote vote = (Vote) msg.getObject();

            service.create(vote);

        }
        catch (Throwable t)
        {
            log.error(t.getMessage());
        }
    }

    public void delete(String aPIN) throws Exception
    {
        // TODO Auto-generated method stub

    }

    public List<Vote> findByGroup(String aGroup) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    public Vote find(String aPIN) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    public List<Vote> findByLector(String aLector) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    public List<Vote> findByLectorAndGroup(String aLector, String aGroup) throws Exception
    {
        // TODO Auto-generated method stub
        return null;
    }

    public void update(Vote aVote) throws Exception
    {
        // TODO Auto-generated method stub

    }

}
